package com.swjd.binarysorttree;

/**
 * 二叉排序树
 */
public class BinarySortTreeDemo {
    public static void main(String[] args) {
        int[] arr = {0, 7, 3, 10, 12, 5, 1, 9};
        BinarySortTree binarySortTree = new BinarySortTree();
        for (int i = 0; i < arr.length; i++) {
            binarySortTree.add(new Node(arr[i]));
        }
        binarySortTree.infixOrder();
        binarySortTree.delete(10);
        binarySortTree.delete(1);
        binarySortTree.infixOrder();
    }
}

/**
 * 二叉树
 */
class BinarySortTree {
    private Node root;

    /**
     * 添加节点
     *
     * @param node
     */
    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    /**
     * 中序遍历
     */
    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("树空");
        }
    }

    /**
     * 删除节点
     *
     * @param value 要删除的值
     */
    public void delete(int value) {
        if (value == root.value && root.right != null) {
            Node node = root.right;
            while (true) {
                if (node.left == null) {
                    break;
                }
                node = node.left;
            }
            node.left = root.left;
            root = root.right;
        } else if (value == root.value && root.left != null && root.right == null) {
            root = root.left;
        } else if (root.right == null && root.left == null) {
            root = null;
        } else {
            root.delete(value);
        }
    }

}

/**
 * 节点
 */
class Node {
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    /**
     * 添加节点
     *
     * @param node 传入的节点信息
     */
    public void add(Node node) {
        if (node == null) {
            return;
        }
        if (node.value < this.value) {
            if (this.left == null) {
                //左子节点为空
                this.left = node;
            } else {
                this.left.add(node);
            }
        }
        if (node.value >= this.value) {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
    }

    /**
     * 中序遍历
     */
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    /**
     * 删除节点
     */
    public void delete(int value) {
        //如果右子节点匹配到了
        if (this.right != null && this.right.value == value) {
            if (this.right.left != null && this.right.right == null) {
                this.right = this.right.left;
                return;
            } else {
                Node node = this.right.right;
                while (node != null) {
                    if (node == null || node.left == null) {
                        break;
                    }
                    node = node.left;
                }
                if (node != null) {
                    node.left = this.right.left;
                }
                //左节点为空时，不管右节点是否为空，都可以把右节点提上去,当左节点不为空时，右节点不为空才能将右节点提上去
                this.right = this.right.right;
                return;
            }
        }
        //如果左子节点匹配到了
        if (this.left != null && this.left.value == value) {
            if (this.left.left != null && this.left.right == null) {
                this.left = this.left.left;
                return;
            } else {
                Node node = this.left.right;
                while (node != null) {
                    if (node == null || node.left == null) {
                        break;
                    }
                    node = node.left;
                }
                if (node != null) {
                    node.left = this.left.left;
                }
                //左节点为空时，不管右节点是否为空，都可以把右节点提上去,当左节点不为空时，右节点不为空才能将右节点提上去
                this.left = this.left.right;
                return;
            }
        }
        if (this.right != null) {
            this.right.delete(value);
        }
        if (this.left != null) {
            this.left.delete(value);
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
}